//定义
import {defineStore} from "pinia";
import {login} from "@/api/login";

const useLoginStore = defineStore("login", {
  state: () => ({

    username: sessionStorage.getItem("username")??"",
/*    id:parseInt(sessionStorage.getItem("id")??""),*/
    id: Number(sessionStorage.getItem("id")??""),
    token: sessionStorage.getItem("token")??""
  }),
  actions: {
    loginAction(loginForm){
     return  login(loginForm).then(res=>{
       this.id = res.data.id
       this.username = res.data.username
       this.token =res.data.token
        //注意sessionStorage localStorage key是字符串，value也是
        sessionStorage.setItem("id",res.data.id)
        sessionStorage.setItem("username",res.data.username)
        sessionStorage.setItem("token",res.data.token)

        return res
      }).catch(e=>{
        throw e
      })

    }
  }
})
export default useLoginStore
